package com.icez.base_util_library

import android.content.res.ColorStateList
import android.graphics.Typeface
import android.os.Build
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.*
import android.view.View
import android.widget.TextView
import androidx.annotation.RequiresApi
import java.util.*

/**
 *  字符串工具类
 * @author icez
 */
class StringUtil {
    companion object{
        val instance:StringUtil by lazy(mode=LazyThreadSafetyMode.SYNCHRONIZED){
            StringUtil()
        }
    }

    /**
     *  不分大小写 包含
     * @param original 原始数据
     * @param target 目标数据
     */
    public fun containsCaseInsensitive(original:String,target:String):Boolean{
        if(original.isNullOrEmpty()||target.isNullOrEmpty()){
            return false
        }
        return original.uppercase().contains(target.uppercase()) || original.contains(target)
    }

    /**
     *  不分大小写匹配
     * @param original 原始数据
     * @param target 目标数据
     */
    public fun equalsCaseInsensitive(original:String,target:String):Boolean{
        if(original.isNullOrEmpty()||target.isNullOrEmpty()){
            return false
        }
        return original.uppercase() == target.uppercase() || original == target
    }

    /**
     * 设置文本的SpannableString，注意要监听点击事件，需要给TextView设置 textView.movementMethod = LinkMovementMethod.getInstance()
     * @param source 完整的内容
     * @param styleText 完整内容中，需要变色的内容
     * @param color 内容的颜色。Color()值
     * @param onClickCallback 点击变色内容的监听
     */
    @Deprecated("淘汰")
    public fun getSpannableStringClickable(
        source: String,
        styleText: String,
        color: Int=0,
        onClickCallback: (() -> Unit?)? = null
    ): SpannableStringBuilder {
        val style = SpannableStringBuilder()
        try{
            var start = source.indexOf(styleText)
            var end = start + styleText.length
            style.append(source)
            if (onClickCallback != null) {
                style.setSpan(object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        onClickCallback()
                    }
                }, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            if(color!=0){
                val foregroundColorSpan = ForegroundColorSpan(color)
                style.setSpan(foregroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
        }catch (e:Exception){
            style.append(source)
        }

        return style
    }




}